package com.galleryviewpager.c_circleprecent;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

import androidx.annotation.Nullable;

import com.galleryviewpager.DensityUtils;
import com.galleryviewpager.R;

public class CirclePrecentView extends View {

    private static final String TAG = "CirclePrecentView";

    private Context mContext;
    private float mStripeWidth;
    private int mCurPercent;
    private int mSmallColor;
    private int mBigColor;
    private int mCenterTextSize;
    private int mRadius;

    private int mWidth, mHeight;  //视图的大小
    private int centerX, centerY; //中心点

    private Paint mBigPaint = new Paint();

    private Paint mSmallPaint = new Paint();

    private int mEndAngle;

    private RectF arcRectF = new RectF();
    private String showText;


    public CirclePrecentView(Context context) {
        this(context, null);
    }

    public CirclePrecentView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CirclePrecentView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.mContext = context;

        TypedArray typedArray = mContext.obtainStyledAttributes(attrs, R.styleable.CirclePrecentView, defStyleAttr, 0);

        //获取彩色带的宽度
        mStripeWidth = typedArray.getDimension(R.styleable.CirclePrecentView_stripeWidth, DensityUtils.dip2px(mContext, 100));

        //获取当前的百分比
        mCurPercent = typedArray.getInteger(R.styleable.CirclePrecentView_percent, 0);

        mSmallColor = typedArray.getColor(R.styleable.CirclePrecentView_smallColor, 0xffafb4db);

        mBigColor = typedArray.getColor(R.styleable.CirclePrecentView_bigColor, 0xff6950a1);

        mCenterTextSize = typedArray.getDimensionPixelSize(R.styleable.CirclePrecentView_centerTextSize, DensityUtils.sp2px(mContext, 20));

        mRadius = typedArray.getDimensionPixelSize(R.styleable.CirclePrecentView_radius, DensityUtils.dip2px(mContext, 100));

        postDelayed(runnable, 1000);

        typedArray.recycle();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int currentWidthSize = MeasureSpec.getSize(widthMeasureSpec);
        int currentWidthMode = MeasureSpec.getMode(widthMeasureSpec);
        switch (currentWidthMode) {
            case MeasureSpec.EXACTLY:
                mWidth = currentWidthSize;
                centerX = currentWidthSize / 2;
                break;
            case MeasureSpec.AT_MOST:
                mWidth = mRadius * 2;
                centerX = mRadius;
                break;
        }

        int currentHeightSize = MeasureSpec.getSize(heightMeasureSpec);
        int currentHeightMode = MeasureSpec.getMode(heightMeasureSpec);
        switch (currentHeightMode) {
            case MeasureSpec.EXACTLY:
                mHeight = currentHeightSize;
                centerY = currentHeightSize / 2;
                break;
            case MeasureSpec.AT_MOST:
                mHeight = mRadius * 2;
                centerY = mRadius;
                break;
        }

        arcRectF.set(0, 0, mWidth, mHeight);
        setMeasuredDimension(mWidth, mHeight);
    }

    @Override
    protected void onDraw(Canvas canvas) {

        mEndAngle = (int) (mCurPercent * 3.6);
        mBigPaint.setAntiAlias(true);
        mBigPaint.setColor(Color.BLACK);
        Log.e(TAG, "onDraw: centerX: " + centerX + " centerY: " + centerY + " radius: " + mRadius);
        canvas.drawCircle(centerX, centerY, mRadius, mBigPaint);

        mBigPaint.setColor(Color.RED);
        canvas.drawArc(arcRectF, 0, mEndAngle, true, mBigPaint);

        mSmallPaint.setColor(Color.BLACK);
        canvas.drawCircle(centerX, centerY, mRadius - mStripeWidth, mSmallPaint);

        showText = "百分比" + mCurPercent + "%";

        mSmallPaint.setColor(Color.GREEN);
        mSmallPaint.setTextSize(16f);
        float measureText = mSmallPaint.measureText(showText);
        canvas.drawText(showText, centerX - measureText / 2, centerY, mSmallPaint);

    }

    private Runnable runnable = new Runnable() {
        @Override
        public void run() {
            if (mCurPercent >= 100) {
                mCurPercent = 10;
            } else {
                mCurPercent += 1;
            }
            postInvalidate();
            postDelayed(runnable, 200);
        }
    };

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        removeCallbacks(runnable);
    }
}
